package cn.edu.zafu.bigdata.llc;

import java.net.*;
import java.io.*;

public class UDPClient {
    public static void main(String[] args) {
        try {
            String hostname = "localhost"; // 服务器地址，如果是连接到同学的服务器，替换为同学的IP地址
            int port = 1234; // 服务器端口号

            DatagramSocket clientSocket = new DatagramSocket();
            String sentence = "Current time, please"; // 请求当前时间
            byte[] sendData = sentence.getBytes();
            byte[] receiveBuffer = new byte[1024];

            InetAddress IPAddress = InetAddress.getByName(hostname);
            DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, port);
            clientSocket.send(sendPacket);

            DatagramPacket receivePacket = new DatagramPacket(receiveBuffer, receiveBuffer.length);
            clientSocket.receive(receivePacket);
            String response = new String(receivePacket.getData(), 0, receivePacket.getLength());
            System.out.println("Server says: " + response);

            clientSocket.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
